// scaup - Scala up!
// (c) 2008-2010, Normen Müller, normen.mueller@gmail.com  
// $Id: streams.scala 80 2010-05-13 12:31:58Z normen.mueller $
package scaup.io

import java.io._
import java.util.zip._

import scaup.Views._
import scaup.data._

/**
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.5
 */
object StreamUtil {  
  def unzip(zip: InputStream, outdir: File) {
    val zis = new java.util.zip.ZipInputStream(new BufferedInputStream(zip))
    val buf = new Array[Byte](65536)
    for(entry <- IteratorHelp.nonNull { zis.getNextEntry() }) {
      val f = outdir/entry.getName()
      if(! entry.isDirectory) {
        val dest = new BufferedOutputStream(new FileOutputStream(f), 65536)
        for(cnt <- IteratorHelp.nonNegative{zis.read(buf)}) dest.write(buf, 0, cnt)
        dest.flush()
        dest.close()
      }
      else if(!f.mkdir()) throw new IOException("Couldn't create directory")
    }
    zis.close()
  }  
}
